package com.hyt.it.ogt.ykcj.service.analysis.impl;

import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.hyt.it.ogt.ykcj.common.enums.CommonEnums;
import com.hyt.it.ogt.ykcj.domain.vo.analysis.AnalysisVo;
import com.hyt.it.ogt.ykcj.domain.vo.analysis.BaseAnalysisVo;
import com.hyt.it.ogt.ykcj.domain.vo.analysis.DynamicColumnVo;
import com.hyt.it.ogt.ykcj.service.grade.impl.ExcelHead;

import lombok.extern.slf4j.Slf4j;

/**
 * 分析数据导出excle工具类
 *
 * @author yaojian
 * @date 2021-11-24
 */
@Slf4j
public class AnalysisDataExportUtil {





    /**
     * 获取科目导出的xssfwokbook
     *
     * @param rows
     * @return
     */
    public static XSSFWorkbook exportBaseAnalysisExcel(List<BaseAnalysisVo> rows, AnalysisVo analysisVo, String sheetName) {
        //1.创建XSSFWorkbook，一个XSSFWorkbook对应的Excel文件
        XSSFWorkbook wb = new XSSFWorkbook();
        //2.在work中添加一个sheet,对应excel文件中的sheet
        XSSFSheet sheet = wb.createSheet(sheetName);

        XSSFFont titleFont = wb.createFont();
        titleFont.setBold(true);

        //单元格样式设置
        XSSFCellStyle alignStyle = wb.createCellStyle();
        alignStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
        alignStyle.setVerticalAlignment(VerticalAlignment.CENTER);//水平居中
        alignStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        alignStyle.setFillForegroundColor(IndexedColors.LIME.getIndex());
        alignStyle.setBorderBottom(BorderStyle.THIN);
        alignStyle.setBorderLeft(BorderStyle.THIN);
        alignStyle.setBorderRight(BorderStyle.THIN);
        alignStyle.setBorderTop(BorderStyle.THIN);
        alignStyle.setFont(titleFont);
        alignStyle.setWrapText(true);

        //3.设置表头，即每个列的列名
        //创建第一行
        XSSFRow row = sheet.createRow(0);
        row.setHeight((short) 500);
        //将列名写入
        List<ExcelHead> titleList = new LinkedList<>();
        titleList.add(new ExcelHead(1, "序号"));
        if (CommonEnums.ANALYSIS_TYPE_SBUJECT.getCode().equals(analysisVo.getType())) {
            titleList.add(new ExcelHead(1, "科目名称"));
        } else if (CommonEnums.ANALYSIS_TYPE_EXAM.getCode().equals(analysisVo.getType())) {
            titleList.add(new ExcelHead(1, "考试名称"));
        } else if (CommonEnums.ANALYSIS_TYPE_EXAMROOM.getCode().equals(analysisVo.getType())) {
            titleList.add(new ExcelHead(1, "考点"));
        }else if (CommonEnums.ANALYSIS_TYPE_SEX.getCode().equals(analysisVo.getType())) {
            titleList.add(new ExcelHead(1, "性别"));
        } else if (CommonEnums.ANALYSIS_TYPE_EDUCATION.getCode().equals(analysisVo.getType())) {
            titleList.add(new ExcelHead(1, "学历"));
        } else if (CommonEnums.ANALYSIS_TYPE_PROVINCE.getCode().equals(analysisVo.getType())) {
            titleList.add(new ExcelHead(1, "省份"));
        } else if (CommonEnums.ANALYSIS_TYPE_CITY.getCode().equals(analysisVo.getType())) {
            titleList.add(new ExcelHead(1, "市区"));
        }
        titleList.add(new ExcelHead(1, "参考人数"));
        List<DynamicColumnVo> dynamicColumnVoList = rows.get(0).getDynamicColumnVoList();
        dynamicColumnVoList.forEach(dynamicColumnVo -> {
            titleList.add(new ExcelHead(1, dynamicColumnVo.getColumnName()));
        });
        titleList.add(new ExcelHead(1, "最高分"));
        titleList.add(new ExcelHead(1, "最低分"));
        titleList.add(new ExcelHead(1, "平均分"));
//        if (analysisVo.getType().equals(CommonEnums.ANALYSIS_TYPE_EDUCATION.getCode()) || analysisVo.getType().equals(CommonEnums.ANALYSIS_TYPE_EDUCATION.getCode())) {
            titleList.add(new ExcelHead(1, "平均答题时长（分钟）"));
//        }
        //设置列名的名称和样式
        int currGroup = 1;
        XSSFCellStyle newStyle = null;
        for (int i = 0; i < titleList.size(); i++) {
            XSSFCell curcell = row.createCell(i);
            curcell.setCellValue(titleList.get(i).getHeadName());
            curcell.setCellStyle(alignStyle);
        }
        //写入每行的正式数据
        for (int i = 0; i < rows.size(); i++) {
            //创建行
            row = sheet.createRow(i + 1);
            //在行里插入内容
            row.createCell(0).setCellValue(i + 1);
            row.createCell(1).setCellValue(rows.get(i).getTypeName());
            row.createCell(2).setCellValue(rows.get(i).getParticipantsNum());
            int curr = 3;
            List<DynamicColumnVo> dynamicColumnVos = rows.get(i).getDynamicColumnVoList();
            for (DynamicColumnVo dynamicColumnVo : dynamicColumnVos) {
                row.createCell(curr).setCellValue(dynamicColumnVo.getColumnVal());
                curr++;
            }
            row.createCell(curr).setCellValue(rows.get(i).getMaxScore().setScale(2,BigDecimal.ROUND_HALF_UP).toString());
            row.createCell(curr + 1).setCellValue(rows.get(i).getMinScore().setScale(2, BigDecimal.ROUND_HALF_UP).toString());
            row.createCell(curr + 2).setCellValue(rows.get(i).getAvgScore().setScale(2,BigDecimal.ROUND_HALF_UP).toString());
//            if (analysisVo.getType().equals(CommonEnums.ANALYSIS_TYPE_EDUCATION.getCode()) || analysisVo.getType().equals(CommonEnums.ANALYSIS_TYPE_EDUCATION.getCode())) {
                row.createCell(curr + 3).setCellValue(rows.get(i).getUseTimeStr());
//            }
        }

        /**
         * 设置单元格宽度
         * 上面的操作已经是生成一个完整的文件了，只需要将生成的流转换成文件即可；
         * 下面的设置宽度可有可无，对整体影响不大
         */
        for (int i = 0; i <= titleList.size(); i++) {
            sheet.setColumnWidth(i, 3300);
        }
        return wb;
    }

}
